﻿Public Class Palette_Edit
    Private selected As Integer

    Sub New(subtitle As String, item As Integer)
        If MainForm.conLanguageDe.Checked Then
            System.Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo("de")
        Else
            System.Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo("en")
        End If
        selected = item
        Me.KeyPreview = True
        InitializeComponent()
        grpText.Text = subtitle
    End Sub

    Private Sub Palette_Edit_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Location = New Point(MainForm.Location.X + 30, MainForm.Location.Y + 30)
    End Sub

    Private Sub key_Check(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub butChange_Click(sender As Object, e As EventArgs) Handles butChange.Click
        If selected = -1 Then
            Add()
            If txtName.Text = "" Then Exit Sub
        Else
            Rename()
        End If
        Me.Close()
    End Sub

    Private Sub Add()
        If MainForm.palettes.Count < 12 Then
            Try
                If txtName.Text = "" Then txtName.Text = "*"
                Dim cr As New System.IO.StreamWriter(MainForm.user & "/Palettes/" & txtName.Text & ".blu")
                cr.Close()
                MainForm.palettes.Add(txtName.Text)
                MainForm.ComPalettes_DropDown(Me, New EventArgs)
                MainForm.comPalettes.SelectedIndex = MainForm.palettes.Count - 1
                MainForm.butRename.Enabled = True
                MainForm.butDelete.Enabled = True
                MainForm.panPal.Enabled = True
                MainForm.clear_Palette()
            Catch
                txtName.Clear()
                MessageBox.Show(My.Resources.not_valid_text, My.Resources.not_valid_title)
                txtName.Focus()
                Exit Sub
            End Try
        Else
            MessageBox.Show(My.Resources.delete_first_text, My.Resources.delete_first_title, MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub Rename()
        IO.File.Move(MainForm.user & "/Palettes/" & MainForm.palettes(selected).ToString & ".blu", MainForm.user & "/Palettes/" & txtName.Text & ".blu")
        MainForm.palettes(selected) = txtName.Text
    End Sub

    Private Sub butCancel_Click(sender As Object, e As EventArgs) Handles butCancel.Click
        Me.Close()
    End Sub

    Private Sub conCut_Click(sender As Object, e As EventArgs) Handles conCut.Click
        Clipboard.SetText(txtName.SelectedText)
        txtName.Text = txtName.Text.Remove(txtName.SelectionStart, txtName.SelectionLength)
    End Sub

    Private Sub conCopy_Click(sender As Object, e As EventArgs) Handles conCopy.Click
        Clipboard.SetText(txtName.SelectedText)
    End Sub

    Private Sub conPaste_Click(sender As Object, e As EventArgs) Handles conPaste.Click
        txtName.Text = txtName.Text.Insert(txtName.SelectionStart, Clipboard.GetText)
    End Sub

    Private Sub conDelete_Click(sender As Object, e As EventArgs) Handles conDelete.Click
        txtName.Text = txtName.Text.Remove(txtName.SelectionStart, txtName.SelectionLength)
    End Sub
End Class